#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX 255 

int n = 10;
void input(int arr[], int n) {
    int i;
    srand(time(NULL));

    for (i = 0; i < n; i++) {
        arr[i] = rand() % 100;
    }
}

void sort(int arr[], int n) {
    int i, j, tmp;
    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
                tmp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = tmp;
            }
        }
    }
}

void insert(int arr[], int key) {
    int i;
    for (i = n; i >= 0; i--) {
        if (arr[i - 1] < key) {
            break;
        }
        arr[i] = arr[i - 1];
    }
    arr[i] = key;
    n++;
}

int seek(int arr[], int key) {
    int i;
    for (i = 0; i < n; i++) {
        if (arr[i] == key) {
            break;
        }
    }
    return i + 1;
}

void delet(int arr[], int key) {
    int i, pos;
    pos = seek(arr, key);
    if (pos) {
        for (i = pos - 1; i < n; i++) {
            arr[i] = arr[i + 1];
        }
    }
    n--;
}

void output(int arr[], int n) {
    int i;
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}


int main() {
    int arr[MAX];
    int i, pos, key, opt;
    
    //int arr[] = {6, 2, 1, 9, 3, 4, 5, 0, 7, 8};

    do {
        printf("1. Input\n");
        printf("2. Sort\n");
        printf("3. Insert\n");
        printf("4. Seek\n");
        printf("5. Delete\n");
        printf("6. Output\n");
        printf("Operation: ");
        scanf("%d", &opt);
        switch(opt) {
            case 1: {
                input(arr, n);
                output(arr, n);
                break;
            }
            case 2: {
                sort(arr, n);
                output(arr, n);
                break;
            }
            case 3: {
                scanf("%d", &key);
                insert(arr, key);
                output(arr, n);
                break;
            }
            case 4: {
                scanf("%d", &key);
                pos = seek(arr, key);
                printf("pos: %d\n", pos);
                break;
            }
            case 5: {
                scanf("%d", &key);
                delet(arr, key);
                output(arr, n);
                break;
            }
            case 6: {
                output(arr, n);
                break;
            }
        }
        printf("-------\n");
    } while (opt > 0 && opt < 7);         
    return 0;
}
